﻿--[[
   @brief    descprtion: badge_levelup_control
   @author   hanmanhuang
   @version  1.0
   @date     2016-07-08
--]]

badge_levelup_control = 
{
    page_info = {};
    init = function()
        badge_levelup_control.inner_log("badge_levelup_control init");
        ierd_tgp.tpf_ui.Require("badge\\badge_levelup\\view.lua");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[badge_levelup_control] %s", msg), core_msg);
    end;

    get_page_url = function()
        local url = badge_model.get_badge_levelup_page_url(badge_levelup_control.page_info);
        local url_login = web_utility.make_login_status_url(url, login_model.main_uin);
        return url_login;
    end;

    badge_levelup_tips_layout = function (win_crtl_id, opts)
        if nil == opts or nil == opts.user_data or Tenio.INVALID_CONTROLID == win_crtl_id then
            return;
        end;

        local pWebControl = ui_utility.get_ctrl_ptr_by_path(win_crtl_id, "web_con");
        if nil ~= pWebControl then
            local pQBWeb = tolua.cast(pWebControl, "Tenio::IQBWebBrowser");
            local url = badge_levelup_control.get_page_url();
            local const_ui_str = U8toCUIStr(url);
            pQBWeb:Navigate(const_ui_str);
        end
    end;

    open_win = function(page_info)
        badge_levelup_control.page_info = page_info;
        badge_levelup_view.open_page();
        -- local info = {};
        -- info.user_data = page_info;
        -- info.position = 'center';
        -- info.width = 340;
        -- info.height = 352;
        -- info.modal = false;
        -- info.is_multi_win = true;
        -- info.check_game_state = true;
        
        -- info.manuallyCloseCallback = function(win_crtl_id)
        --     badge_levelup_control.close_wnd(0, win_crtl_id);
        -- end;

        -- info.ui_file = "BadgeLeveUpWnd.tpf";
        -- info.layout_callback = "badge_levelup_control.badge_levelup_tips_layout";
        -- info.layout_file = "badge\\badge_levelup\\control.lua";
        -- ui_utility.show_tips(info);
    end;

    close_wnd = function(uilib, win_ctrl_id)
        badge_levelup_control.page_info = {};
        badge_levelup_view.close_page();
    end;

    on_document_complete = function()
    end;

    on_js_callback = function(uilib, fuc_name, json, web_ctrl_id)
        badge_levelup_control.inner_log("on_js_callback.fuc_name="..fuc_name);
        local win_ctrl_id = ui_utility.get_ctrl_parent_id(web_ctrl_id);

        if fuc_name == "close_window" then
            badge_levelup_control.close_wnd(uilib, win_ctrl_id);
        elseif fuc_name == "close_dlg" then
            badge_levelup_control.close_wnd(uilib, win_ctrl_id);
        elseif fuc_name == "get_badge_info" then
            local param = base_utility.json_to_table(json);
            if nil ~= param and nil ~= param.callback_func and param.callback_func ~= "" then
                local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
                rsp_table.data = {};
                if badge_model.con_badge_oss_cfg ~= nil then
                    rsp_table.data.oss_cfg = badge_model.con_badge_oss_cfg;
                    rsp_table.data.user_info = badge_model.get_user_base_info();
                end;
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_by_id_ex(param.callback_func, rsp_json, web_ctrl_id);
            end;
        elseif fuc_name == "i_service_social_open_personal_center" then
            local info = {};
            info.func_name = fuc_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_MAINFRAME,
                                    base_utility.table_to_json(info));
        else 
            badge_levelup_control.inner_log("on_js_callback other event!");
        end;
    end;
};

badge_levelup_control.init();